package panel.common;

import Base.TextField;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class PasswordField extends TextField {
    private boolean showPassword = false;
    private JPasswordField passwordField = new JPasswordField();
    JButton showButton = new JButton(new ImageIcon("src/main/resources/icon/login/hide.png"));
    JButton hideButton = new JButton(new ImageIcon("src/main/resources/icon/login/hide.png"));

    public PasswordField(String name) {
        this.name=name;
        init();
    }

    private void init() {
        super.init(name);

        // 文本框
        passwordField.setText(name);
        passwordField.setBorder(null);
        passwordField.setBounds(64, 0, getWidth() - 64 - 64 - 10, 64);
        passwordField.setForeground(Color.gray);
        passwordField.setFont(font);
        passwordField.setEchoChar((char) 0);     // 默认显示字符
        passwordField.setHorizontalAlignment(SwingConstants.CENTER);
        add(passwordField);

        // 显示密码按钮
        showButton.setBounds(5, 0, 64, 64);
        showButton.setBackground(Color.white);
        showButton.setVisible(false);
        showButton.setBorder(null);
        showButton.addActionListener(e -> {
            passwordField.setEchoChar((char) 0);
            showPassword = true;
            showButton.setVisible(false);
            hideButton.setVisible(true);
        });
        add(showButton);

        // 隐藏密码按钮
        hideButton.setBounds(5, 0, 64, 64);
        hideButton.setBackground(Color.white);
        hideButton.setVisible(false);
        hideButton.setBorder(null);
        hideButton.addActionListener(e -> {
            passwordField.setEchoChar('*');
            showPassword = false;
            hideButton.setVisible(false);
            showButton.setVisible(true);
        });
        add(hideButton);

       clearButton.addActionListener(e -> {
            passwordField.setText("");
        });
        // 焦点监听
        passwordField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                clearButton.setVisible(true);
                passwordField.setForeground(Color.black);
                if (showPassword) {
                    passwordField.setEchoChar((char) 0);
                    hideButton.setVisible(true);
                } else {
                    passwordField.setEchoChar('*');
                    showButton.setVisible(true);
                }
                passwordField.setText(data);
            }

            @Override
            public void focusLost(FocusEvent e) {
                clearButton.setVisible(false);
                showButton.setVisible(false);
                hideButton.setVisible(false);
                data = passwordField.getText();
                if (data.isEmpty()) {
                    passwordField.setText(name);
                    passwordField.setEchoChar((char) 0);
                    passwordField.setForeground(Color.gray);
                } else {
                    passwordField.setForeground(Color.black);
                    if (showPassword) {
                        passwordField.setEchoChar((char) 0);
                    } else {
                        passwordField.setEchoChar('*');
                    }
                }
            }
        });
    }

    public void setMaxLength(int maxLength) {
        passwordField.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                if (passwordField.getText().length() >= maxLength) {
                    e.consume();
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
                //do nothing
            }

            @Override
            public void keyReleased(KeyEvent e) {
                //do nothing
            }
        });
    }

    public String getText() {
        return data;
    }

    public void clear() {
        passwordField.setText("");
    }
}
